package com.wuhuahe.learning.multitable.mapper;

import com.wuhuahe.learning.multitable.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

//@CacheNamespace
public interface IUserMapper {

    List<User> findUserWithOrders();

    @Select("select * from public.user")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "orders", column = "id", javaType = List.class,
                    many = @Many(select = "com.wuhuahe.learning.multitable.mapper.IOrderMapper.selectOrderByUid"))
    })
    List<User> findUserWithOrdersWithAnnotation();


    List<User> findUserWithRoles();

    @Select("select * from public.user")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "roles", column = "id", javaType = List.class,
                    many = @Many(select = "com.wuhuahe.learning.multitable.mapper.IRoleMapper.findRoleByUid"))
    })
    List<User> findUserWithRolesWithAnnotation();

    @Insert("insert into public.user values(#{id}, #{username})")
    void insertUser(User user);

    @Update("update public.user set username=#{username} where id=#{id}")
    void updateUser(User user);

    @Select("select * from public.user")
    List<User> selectUsers();

    @Delete("delete from public.user where id=#{id}")
    void deleteUser(Integer id);

    @Select("select * from public.user where id=#{id}")
    User selectUserById(Integer id);
}
